package com.jh3502.vivo.function.tododetail.travel;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.base.BaseCallback;
import com.jh3502.vivo.base.BaseJson;
import com.jh3502.vivo.base.BasePresenter;
import com.jh3502.vivo.base.BaseView;
import com.jh3502.vivo.function.travelnew.TravelNewBean;
import com.jh3502.vivo.utils.Utils;

import java.util.List;
import java.util.Map;

/**
 * @author harisucici
 * @date 2017/6/19
 */

public class TodoTravelPresenter extends BasePresenter<TodoTravelPresenter.TodoDetailView> {
    TodoTravelModel mTodoDetailModel;

    TodoDetailView mTodoDetailView;


    public void setTodoDetailView(TodoDetailView mTodoDetailView) {
        this.mTodoDetailView = mTodoDetailView;

    }

    public void toGetTravelById(Map<String, Object> body) {
        mTodoDetailView.showLoadingDialog();
        if (mTodoDetailModel == null) {
            mTodoDetailModel = new TodoTravelModel(mTodoDetailView.getActContext());
        }

        mTodoDetailModel.getTravelById(body, new BaseCallback(mTodoDetailView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                mTodoDetailView.getTravelByIdSuccess(new Gson().fromJson(baseJson.getObj().toString(), TravelNewBean.class));
            }
        });
    }

    public void toGetUser(Map<String, Object> body) {
        mTodoDetailView.showLoadingDialog();
        if (mTodoDetailModel == null) {
            mTodoDetailModel = new TodoTravelModel(mTodoDetailView.getActContext());
        }

        mTodoDetailModel.getUser(body, new BaseCallback(mTodoDetailView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {
                mTodoDetailView.getUserSuccess(Utils.getInstance().jso2map(baseJson.getObj().toString()));
            }
        });
    }

    public void toGetChangeAndAuditRecords(Map<String, Object> body) {
        mTodoDetailView.showLoadingDialog();
        if (mTodoDetailModel == null) {
            mTodoDetailModel = new TodoTravelModel(mTodoDetailView.getActContext());
        }

        mTodoDetailModel.getChangeAndAuditRecords(body, new BaseCallback(mTodoDetailView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {
                List<TodoDetailChangeBean> beans = new Gson().fromJson(baseJson.getObj().toString(),
                        new TypeToken<List<TodoDetailChangeBean>>() {
                        }.getType());
                mTodoDetailView.getChangeAndAuditRecordsSuccess(beans);
            }
        });
    }

    public void toAgreeForAll(List<Map<String, Object>> body) {
        mTodoDetailView.showLoadingDialog();
        if (mTodoDetailModel == null) {
            mTodoDetailModel = new TodoTravelModel(mTodoDetailView.getActContext());
        }

        mTodoDetailModel.agreeForAll(body, new BaseCallback(mTodoDetailView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mTodoDetailView.agreeForAllSuccess();
            }
        });
    }

    public void toDeleteAudit(Map<String, Object> body) {
        mTodoDetailView.showLoadingDialog();
        if (mTodoDetailModel == null) {
            mTodoDetailModel = new TodoTravelModel(mTodoDetailView.getActContext());
        }

        mTodoDetailModel.deleteAudit(body, new BaseCallback(mTodoDetailView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mTodoDetailView.deleteAuditSuccess();
            }
        });
    }

    public void toDeleteTask(Map<String, Object> body) {
        mTodoDetailView.showLoadingDialog();
        if (mTodoDetailModel == null) {
            mTodoDetailModel = new TodoTravelModel(mTodoDetailView.getActContext());
        }

        mTodoDetailModel.deleteTask(body, new BaseCallback(mTodoDetailView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mTodoDetailView.deleteTaskSuccess();
            }
        });
    }


    @Override
    public void cancelCall() {

    }

    public interface TodoDetailView extends BaseView {
        void getTravelByIdSuccess(TravelNewBean travelNewBean);

        void getUserSuccess(Map<String, Object> map);

        void getChangeAndAuditRecordsSuccess(List<TodoDetailChangeBean> beans);

        void agreeForAllSuccess();

        void deleteAuditSuccess();

        void deleteTaskSuccess();
    }
}
